package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.knowledge.domain.dto.SysFormDocFileDto;
import cn.cdeden.knowledge.domain.dto.SysFormDocFileQueryParam;
import cn.cdeden.knowledge.utils.PageUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cdeden.knowledge.domain.SysFormDocFile;
import cn.cdeden.knowledge.mapper.SysFormDocFileMapper;
import cn.cdeden.knowledge.service.SysFormDocFileService;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
public class SysFormDocFileServiceImpl extends ServiceImpl<SysFormDocFileMapper, SysFormDocFile> implements SysFormDocFileService{

    private final SysFormDocFileMapper sysFormDocFileMapper;
    @Override
    public TableDataInfo<SysFormDocFileDto> queryAll(SysFormDocFileQueryParam query, Pageable pageable) {
        IPage<SysFormDocFile> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<SysFormDocFile> page = sysFormDocFileMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return MapstructUtils.convertTableDataInfo(TableDataInfo.build(page), SysFormDocFileDto.class);
    }

    @Override
    public List<SysFormDocFileDto> queryAll(SysFormDocFileQueryParam query){
        return MapstructUtils.convert(sysFormDocFileMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), SysFormDocFileDto.class);
    }

    @Override
    public SysFormDocFile getById(Integer id) {
        return sysFormDocFileMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public SysFormDocFileDto findById(Integer id) {
        return MapstructUtils.convert(getById(id), SysFormDocFileDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(SysFormDocFileDto resources) {
        SysFormDocFile entity = MapstructUtils.convert(resources, SysFormDocFile.class);
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(SysFormDocFileDto resources){
        SysFormDocFile entity = MapstructUtils.convert(resources, SysFormDocFile.class);
        boolean ret = this.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Set<Integer> ids){
        // delCaches(ids);
        return this.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.deleteByIds(set);
    }

    @Override
    public boolean deleteByFormDocId(Integer formDocId) {
        Map<String, Object> map = new HashMap<>();
        map.put("form_doc_id", formDocId);
        return this.removeByMap(map);
    }
}
